//
//  MainViewController.swift
//  weibo---练习
//
//  Created by 胡海波 on 15/6/24.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // 设置按钮的总数
    private let buttonCount = 5

    override func viewDidLoad() {
        super.viewDidLoad()
       
        addChildViewControllers()
    }

    // 在viewDidAppear方法中设置添加按钮
    override func viewDidAppear(animated: Bool) {


        let w = tabBar.bounds.width / CGFloat(buttonCount)

        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)

        // 添加 “ + ” 按钮的位置
        composedButton.frame = CGRectOffset(rect, w * 2, 0)
        
    }
    func composedButtonClicked() {

        print("\(__FUNCTION__)")
    }


    // 添加子控制器
   private func addChildViewControllers() {

        addChildViewController(HomeViewController(), title: "首页" ,imageName: "tabbar_home")

        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")

        addChildViewController(UIViewController(), title: "", imageName: nil)
    
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
    
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")

}

    private func addChildViewController(vc:UIViewController,title: String,imageName: String?) {


        vc.title = title

        if imageName != nil {
       // 设置控制器属性
            vc.tabBarItem.image = UIImage(named: imageName!)
            vc.tabBarItem.selectedImage = UIImage(named: imageName! + "_highlighted")
        }

        // 创建导航控制器并初始化vc为根控制器
        let nav = UINavigationController(rootViewController: vc)

        addChildViewController(nav)

    }

    lazy var composedButton: UIButton = {

        let btn = UIButton()

        // 设置btn的属性
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        btn.addTarget(self, action: "composedButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

        self.tabBar.addSubview(btn)
        
        return btn
        }()


}
